package com.caiheng.api.service.estate.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.EstateChatApplyMapper;
import com.caiheng.api.dao.estate.EstateChatImgMapper;
import com.caiheng.api.dao.estate.EstateChatMapper;
import com.caiheng.api.dao.estate.EstateMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.estate.EstateChat;
import com.caiheng.api.entity.estate.EstateChatApply;
import com.caiheng.api.entity.estate.EstateChatImg;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.estate.EstateChatApplyService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.util.DateUtil;
import com.caiheng.api.util.UuidUtil;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 物业接待回复Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-16
 */
@Service
public class EstateChatApplyServiceImpl extends BaseServiceImpl<EstateChatApplyMapper, EstateChatApply> implements EstateChatApplyService {

    @Resource
    private EstateChatApplyMapper chatApplyMapper;
    @Resource
    private EstateChatMapper chatMapper;
    @Resource
    private EstateChatImgMapper chatImgMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private EstateMapper estateMapper;

    @Value("${web.imgPrefix}")
    private String imgPrefix;

    @Override
    @Transactional
    public String addApply(User user, EstateChat chat, String apply_id, String content) {
        String applyId = null;
        try{
            EstateChatApply chatApply = new EstateChatApply();
            chatApply.setId(UuidUtil.get32UUID());
            chatApply.setChatId(chat.getId());
            chatApply.setCreateTime(new Date());
            if(StrUtil.isNotEmpty(apply_id)){
                chatApply.setParentId(apply_id);
            }
            chatApply.setContent(content);
            chatApply.setUserId(user.getId());
            int ret = chatApplyMapper.insert(chatApply);
            if(ret <= 0){

            }

            applyId = chatApply.getId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return applyId;
    }

    @Override
    public List<EstateChatApply> getListByChatId(int chatId) {
        QueryWrapper<EstateChatApply> qwRecord = new QueryWrapper<>();
        qwRecord.eq("chat_id", chatId);
        qwRecord.orderByAsc("create_time");
        List<EstateChatApply> applyList = chatApplyMapper.selectList(qwRecord);
        if(CollUtil.isNotEmpty(applyList)){
            for (EstateChatApply apply : applyList){
                User user1 = userMapper.selectById(apply.getUserId());
                if(user1!=null){
                    if(StrUtil.isNotEmpty(user1.getNickName())){
                        apply.setNickName(EmojiParser.parseToUnicode(user1.getNickName()));
                    }
                    apply.setFaceUrl(user1.getFaceUrl());
                }

                apply.setTimeStr(DateUtil.getDateStr(apply.getCreateTime()));

                QueryWrapper<EstateChatImg> qwRecordImg = new QueryWrapper<>();
                qwRecordImg.eq("chat_id",chatId);
                qwRecordImg.eq("img_type",1);
                List<EstateChatImg> recordImgs = chatImgMapper.selectList(qwRecordImg);
                if(CollUtil.isNotEmpty(recordImgs)){
                    for (EstateChatImg img : recordImgs){
                        if(StrUtil.isNotEmpty(img.getImgUrl())){
                            img.setImgUrl(imgPrefix + img.getImgUrl());
                        }
                    }
                    apply.setChatImgs(recordImgs);
                }
            }
        }
        return applyList;
    }
}
